package com.qyhl.webtv.module_live.teletext.detail.popview.detail;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.live.ShoppingListBean;
import com.qyhl.webtv.commonlib.entity.live.ShoppingUserInfoBean;
import com.qyhl.webtv.module_live.common.LiveUrl;

public class TeleTextShopDetailModel implements TeleTextShopDetailContract.TeleTextShopDetailModel {
    private TeleTextShopDetailPresenter mPresenter;

    public TeleTextShopDetailModel(TeleTextShopDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(String id) {
        EasyHttp.get(LiveUrl.GET_TELETEXT_SHOP_DETAIL)
                .params("itemId", id)
                .execute(new SimpleCallBack<ShoppingListBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何内容！");
                        } else {
                            mPresenter.setError("请求失败，请重新获取！");
                        }
                    }

                    @Override
                    public void onSuccess(ShoppingListBean bean) {
                        mPresenter.setDetail(bean);
                    }
                });
    }

    @Override
    public void getStock(String id) {
        EasyHttp.get(LiveUrl.GET_TELETEXT_SHOP_STOCK)
                .params("itemId", id)
                .execute(new SimpleCallBack<Integer>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setStock(0);
                    }

                    @Override
                    public void onSuccess(Integer integer) {
                        mPresenter.setStock(integer);
                    }
                });
    }

    @Override
    public void getUserInfo(String userName) {
        EasyHttp.get(LiveUrl.GET_TELETEXT_SHOP_USER_INFO)
                .params("phone", userName)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<ShoppingUserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setError("获取用户信息失败！");
                    }

                    @Override
                    public void onSuccess(ShoppingUserInfoBean shoppingUserInfoBean) {
                        mPresenter.setUserInfo(shoppingUserInfoBean);
                    }
                });
    }

    @Override
    public void doOrder(String name, String phone, String itemId, String number) {
        EasyHttp.post(LiveUrl.POST_TELETEXT_SHOP_ORDER)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("name", name)
                .params("phone", phone)
                .params("itemId", itemId)
                .params("number", number)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 303) {
                            mPresenter.orderError("积分不足！");
                        } else if (e.getCode() == 304) {
                            mPresenter.orderError("库存不足！");
                        } else {
                            mPresenter.orderError("下单出错，请稍后再试！");
                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s, ApiResult.class);
                        String orderId = (String) apiResult.getData();
                        if (StringUtils.isEmpty(orderId)) {
                            mPresenter.orderError("库存不足！");
                        } else {
                            mPresenter.orderSuccess((String) apiResult.getData());
                        }

                    }
                });
    }
}
